package com.packet.cheng.service.impl;


import com.packet.cheng.dto.ConcatListParamDTO;
import com.packet.cheng.entity.ConcatList;
import com.packet.cheng.entity.User;
import com.packet.cheng.mapper.ConcatListMapper;
import com.packet.cheng.mapper.UserMapper;
import com.packet.cheng.service.ConcatListService;
import com.packet.cheng.util.DateUtil;
import com.packet.cheng.util.R;
import com.packet.cheng.util.StringUtil;
import com.packet.cheng.vo.ConcatListVo;
import com.packet.cheng.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 张在成
 * 好友信息相关
 */
@Service
public class ConcatListServiceImpl implements ConcatListService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ConcatListMapper concatListMapper;


    @Override
    public R<UserVo> searchGoodFriend(ConcatListParamDTO concatList){
        //搜索该好友是否存在
        User user=new User();
        user.setId(concatList.getUserId());
        User user1 = userMapper.selectUserById(user);
        if (StringUtil.isBlank(user1.toString())){
            return R.data(400,"没有此用户");
        }
        UserVo userVo=new UserVo();
        BeanUtils.copyProperties(user1,userVo);
        return R.data(200,"查询成功",userVo);
    }

    @Override
    public R<Boolean> addGoodFriend(ConcatListParamDTO concatListDto) throws Exception {
        //再判断该用户是否已成为通讯录好友
//        ConcatList concatList1 = concatListMapper.searchGoodFriend(concatList.getCommId(), concatList.getUserId());
        /*if (StringUtil.isBlank(concatList1.toString())){
            return R.data(400,"没有该好友");
        }*/
        ConcatListVo concatListVo=new ConcatListVo();
//        BeanUtils.copyProperties(concatList1,concatList);
        //查询该好友列表是否有该用户，有的话不能重复添加

        ConcatList concatList=new ConcatList();
        //concatList.setUserId(userById.getId());
//        concatList.setConcatName(userById.getNickName());
        if(StringUtil.isBlank(concatListDto.getRemarkName())){
//            concatList.setRemarkName(userById.getNickName());
        }else {
            concatList.setRemarkName(concatListDto.getRemarkName());
        }
        //concatList.setSex(userById.getSex());
        //concatList.setAge(userById.getAge());
        concatList.setIsOnline(0);
        concatList.setIsValid(1);
        concatList.setStatus(0);
        concatList.setCeateTime(DateUtil.getCurrentDate(new Date()));
        //进行添加好友,未同意状态设置为0
        boolean b = concatListMapper.insertGoodFriend(concatList);
        if (b){
            return  R.data(200,"已发送添加审请,等待对方确认");
        }else {
            return R.data(200,"添加失败");
        }
    }

    @Override
    public R<List<ConcatList>> selectGoodFriendByUserId(ConcatListParamDTO userParamDTO) throws Exception {
        if (StringUtil.isBlank(userParamDTO.getUserId().toString())){
            return R.data(400,"当前用户不在线");
        }
        List<ConcatList> users = concatListMapper.selectGoodFriendListById(userParamDTO.getUserId());
        if (StringUtil.isBlank(users.toString())){
            return R.data(406,"该用户没有好友");
        }
        return R.data(200,"查询成功",users);
    }
}
